<?php 
    session_start();
//    echo "Login: ".$_SESSION["userID"]." ".$_SESSION["userType"];
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        if(isset($_SESSION["userID"]) && !strcmp($_SESSION["userType"], "administrator")){
        
        $button = $_POST["button"];
        $userID = $_POST["userID"];
        $username = $_POST["username"];
        $password = $_POST["password"];
        $firstName = $_POST["firstName"];
        $lastName = $_POST["lastName"];
        $dob = $_POST["dob"];
        $userType = $_POST["userType"];
        
        if(strcmp($button, "Cancel")){
        $con = mysql_connect("localhost", "zhouzhao", "19831022");
        if(!$con){
            die("could not connect to DB: ".mysql_error());
        }
        
        mysql_selectdb("cloudcom", $con);
        
        if(strlen($userID) == 0){
            mysql_query("insert into users values
            (null, '$username', '$password', '$firstName', '$lastName', '$dob', '$userType')");
//            echo "new user $firstName $lastName is inserted";
        }else{
            mysql_query("update users set username='$username', password='$password', firstName='$firstName', lastName='$lastName',
                    dob='$dob', userType='$userType' where userID=$userID");
//            echo "record $userID is updated";
        }
        
        mysql_close($con);
        
        require 'admin.php';
        }else{
            require 'admin.php';
        }
        }else{
            require 'login.html';
        }
        ?>
    </body>
</html>
